/*
 * 
 * Server-Client multiple file downloader by konstantin
 * gabe.konstantin@gmail.com
 * v0.1
 * 
 * TODO: heavy exception handling
 *       swing surface
 * 
 * Changelog: -
 * 
 * Client side
 * 
 */

package clientpatcher;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ClientPatcher {
    public static void main(String[] argv) throws Exception {
    
        /*
        * Client Language
        * {HU, EN}
        * 
        */
        String language = "EN";
      
        List<String> messageList = new ArrayList<>();
        List<String> fileList = new ArrayList<>();
            fileList.add("msvcr110.dll");
            fileList.add("root.eix");
            fileList.add("root.epk");
            fileList.add("icon.eix");
            fileList.add("icon.epk");
                  
          
          
        if(language.equals("HU")){
            messageList.add("file letoltese sikeres a szerverrol:");
            messageList.add("Patchfileok letoltese kesz!");
        }else if(language.equals("EN")){
            messageList.add("downloaded succesfully from");
            messageList.add("Patching done!");
        }
                  
        //iterate through the filelist
        for(String item : fileList){ //

            Socket socket = new Socket("localhost", 9000);

            BufferedInputStream in = new BufferedInputStream(socket.getInputStream());

            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(item));

            byte[] buffer = new byte[1024 * 50];
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            System.out.println(item + " " + messageList.get(0) + " " + socket.getInetAddress().getHostName());
            in.close();
            out.flush();
            out.close();
            socket.close();
        }
        //downloading completed, close the socket and print a message
        System.out.println("\n" + messageList.get(1));
    }
}